<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
    //当我们用obj.xxx访问一个对象的属性时，JavaScript引擎先在当前对象上查找该属性，
    //如果没有找到，就到其原型对象上找，如果还没有找到，就一直上溯到Object.prototype对象，最后，如果还没有找到，就只能返回undefined。
    var arr = [1,2,3,4]
    //arr ----> Array.prototype ----> Object.prototype ----> null
    function foo(){
        return a
    }
    console.log()
    //foo ----> Function.prototype ----> Object.prototype ----> null
    //构造函数
    function Student(name){
       this.name = name;
       this.hello = function(){
           console.log('my name is '+this.name);
       }
    }
    //这确实是一个普通函数，但是在JavaScript中，可以用关键字new来调用这个函数，并返回一个对象：
    // Object.prototype.speak = function(){
    //     console.log('i want speak');
    // }
    var xiaojie = new Student('xiaojie');
    console.log(xiaojie)
    //xiaoming ----> Student.prototype ----> Object.prototype ----> null
    //用new Student()创建的对象还从原型上获得了一个constructor属性，它指向函数Student本身：
    console.log(xiaojie.constructor === Student.prototype.constructor) //true
    Student.prototype.constructor === Student; // true
    console.log(xiaojie.constructor === Student) //true
    //xiaojie.speak()
    var xiaoxue = new Student('xiaoxue');
    console.log(xiaoxue.hello == xiaojie.hello) //false
    Student.prototype.jump = function(){
        console.log('i im is jump')
    } 
    console.log(xiaoxue.jump == xiaojie.jump) //true
    //调用构造函数千万不要忘记写new。为了区分普通函数和构造函数，按照约定，构造函数首字母应当大写，而普通函数首字母应当小写，
    //这样，一些语法检查工具如jslint将可以帮你检测到漏写的new。
    //class 继承
    class Person{
         constructor(sex){
             this.sex = sex
         }
         hello(){
             console.log('my name is '+this.name)
         }
    }
    class Man extends Person{
        constructor(name){
           super('man')//调用父类的构造方法
           this.name = name;
        }
        speak(){
            console.log('我是'+this.name)
        }
    }
    class Woman extends Person{
        constructor(name){
           super('woman')
           this.name = name;
        }
        speak(){
            console.log('我是'+this.name)
        }
    }
    var man = new Man('宋栋');
    var woman = new Woman('翠花');
    console.log(man)
    console.log(woman)
    man.hello()
    man.speak()
</script>
</body>
</html>